import axios from 'axios'

const instance = axios.create({
  baseURL: 'http://localhost:8181',
  timeout: 5000
})

// 请求拦截器
instance.interceptors.request.use(config => {
  const token = localStorage.getItem('token')
  if (token) {
    config.headers.Authorization = `Bearer ${token}`
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器
instance.interceptors.response.use(response => {
  return response.data
}, error => {
  return Promise.reject(error.response?.data || error)
})

export const dormitoryApi = {
  // 获取宿舍列表
  getList(page, size) {
    return instance.get(`/dormitory/list/${page}/${size}`)
  },
  // 搜索宿舍
  search(params) {
    return instance.get('/dormitory/search', { params })
  },
  // 删除宿舍
  deleteById(id) {
    return instance.delete(`/dormitory/deleteById/${id}`)
  },
  // 添加宿舍
  saveDormitory(data) {
    return instance.post('/dormitory/save', data)
  },
  // 获取单个宿舍详情
  getDormitoryById(id) {
    return instance.get(`/dormitory/findById/${id}`)
  },
  // 更新宿舍信息
  updateDormitory(data) {
    return instance.put('/dormitory/update', data)
  }
}